package library.qrcode.zxing;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;

import java.lang.ref.WeakReference;

/**
 * Created by zhoucl on 2017/8/8 0008.
 */

public class QrCodeHelper {
    private static QrCodeListener qrCodeListener;
    private static WeakReference<Activity> weakReference;

    /**
     * 调起烧苗二维码界面
     * 如果targetSdkVersion>22请自行申请动态权限<br/>
     * Manifest.permission.CAMERA<br/>
     * 推荐使用RxPermissions
     *
     * @param context
     * @param qrCodeListener
     */
    public static void scan(Activity context, QrCodeListener qrCodeListener) {
        QrCodeHelper.qrCodeListener = qrCodeListener;
        weakReference = new WeakReference<>(context);
        context.startActivity(new Intent(context, ScanQRActivity.class));
    }

    public static void result(final String result) {
        if (weakReference.get() != null && qrCodeListener != null) {
            weakReference.get().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    qrCodeListener.result(result);
                }
            });
        }
    }
}
